package com.googlecode.allinclusive.fw.core.config;

import java.io.IOException;
import java.util.Locale;
import java.util.Properties;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.googlecode.allinclusive.fw.core.utils.ClassUtils;

public class GlobalConfig {

	private static final Log log = LogFactory.getLog(GlobalConfig.class);

	public static final String CONFIG_FILE = ClassUtils.toProppertiesPath(GlobalConfig.class);
	public static final String CUSTOM_CONFIG_FILE = ClassUtils.toSimpleProppertiesPath(GlobalConfig.class);
	
	public static final String DEFAULT_ENCODING;
	public static final Locale DEFAULT_LOCALE;
	public static final TimeZone DEFAULT_TIMEZONE;
	public static final String[] I18N_RESOUERCES;
	private static GlobalConfig _self;
	static {
		_self = new GlobalConfig();
		// default encoding
		DEFAULT_ENCODING = GlobalConfig.getProperty("default.encoding", "UTF-8");
		// i18n resource
		String i18n = GlobalConfig.getProperty("i18n.resources");
		if (!StringUtils.isBlank(i18n)) {
			I18N_RESOUERCES = i18n.split(",");
		} else {
			I18N_RESOUERCES = new String[0];
		}
		// default locale
		String strLocale = GlobalConfig.getProperty("default.locale");
		if (StringUtils.isBlank(strLocale)) {
			DEFAULT_LOCALE = Locale.getDefault();
		} else {
			DEFAULT_LOCALE = org.springframework.util.StringUtils.parseLocaleString(strLocale);
		}
		// default timezone
		String strTimezone = GlobalConfig.getProperty("default.timezone");
		if (StringUtils.isBlank(strTimezone)) {
			DEFAULT_TIMEZONE = TimeZone.getDefault();
		} else {
			DEFAULT_TIMEZONE = TimeZone.getTimeZone(strTimezone);
		}
	}
	
	private Properties properties;
	
	private GlobalConfig() {
		properties = load(CONFIG_FILE);
		properties.putAll(load(CUSTOM_CONFIG_FILE));
	}

    public static String getProperty(String key) {
    	return _self.properties.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
    	return _self.properties.getProperty(key, defaultValue);
    }    
    
    public static boolean getBoolean(String key) {
        Boolean b = getBoolean(key, null);
        if (b != null) {
            return b.booleanValue();
        } else {
        	return false;
        }
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        return getBoolean(key, new Boolean(defaultValue)).booleanValue();
    }

    public static Boolean getBoolean(String key, Boolean defaultValue) {
    	String value = getProperty(key);
    	if (StringUtils.isBlank(value)) {
    		return defaultValue;
    	}
    	if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")) {
    		return true;
    	} else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no")) {
    		return false;
    	} else {
    		return defaultValue;
    	}
    }
	
    /**
     * 加载指定的配置文件
     */
	public static Properties load(String resourceName) {
		return load(resourceName, (ClassLoader) null);
	}
	
	/**
	 * 加载指定的配置文件
	 * @param resourceName 配置文件
	 * @param classLoader
	 * @return
	 */
	public static Properties load(String resourceName, ClassLoader classLoader) {
		log.debug(resourceName + " load...");
		
		if (classLoader == null) {
			classLoader = ClassUtils.getDefaultClassLoader();
		}
		
		Properties p = new Properties();
		try {
			p = PropertiesLoaderUtils.loadProperties(new ClassPathResource(resourceName));
		} catch (IOException ioe) {
			log.error(ioe);
		} finally {
		}
		
		return p;
	}
	
	public static class KEY {
		public static final String DEFAULT_BOOLEAN = "boolean.default";
		public static final String DEFAULT_ENCODING = "default.encoding";
		public static final String I18N_RESOURCES = "i18n.resources";
		public static final String DEFAULT_LOCALE = "default.locale";
		public static final String DEFAULT_TIMEZONE = "default.timezone";
	}
}
